package server;

import java.io.PrintWriter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 
 * Server-side handler that manages and executes commands from the
 * network clients in the server
 * 
 */
public class ServerManager implements Runnable {

	public static BlockingQueue<String> commandQueue = new LinkedBlockingQueue<String>();
	
	public void run() {		
		System.out.println("ServerManager Initiated.");
		try {
			while(true){
				System.out.println("server manager processing " + commandQueue.toString());
				process(commandQueue.take());
			}
		} catch (InterruptedException e) {
			System.out.println("in servermanager exception");
			e.printStackTrace();
		}
	}

	/**
	 * Parses a command taken off the commandQueue and executes 
	 * the appropriate method
	 * @param command
	 */
	public static void process(String command) {

		System.out.println("in server manager processing func: " + command);

		// messaging commands
		if (command.startsWith("chatMessage")) 
		{
			String details = command.substring(12, command.length()-1);
			System.out.println("TODO: CHECK ME AT CHAT MESSAGE");
			System.out.println("svrmnger chatmsg details is: " + details);
			String[] params = details.split("%",2);
			System.out.println(params[1]);
			String sender = params[0];
			String outcome = chatMessage(new String[]{sender, params[1]});
			System.out.println("svrmnger chatmsg outcome is: " + outcome + " writing to " + sender);
			Server.clientWriters.get(sender).println(outcome);
		}
		
		if (command.startsWith("startDrag"))
		{
			String[] params = command.split("(")[1].split(",");
			int stackID = Integer.parseInt(params[0]);
			String sender = params[1].substring(0, params[1].length()-1);
			
			
			String outcome = startDrag(params);
			Server.clientWriters.get(sender).println(outcome);
			
			
		}
		
	/*	if (command.startsWith("select"))
		{
			String details = command.substring(7, command.length()-1);
			System.out.println("select details " + details);
			String[] params = details.split(",", 2);
			String stackName = params[0];
			String userName = params[1];
			String outcome = select(new String[]{stackName, userName});
			Server.clientWriters.get(userName).println(outcome);
			                         
			
		}*/
	}


	/**
	 * Sends a text message from one user to another
	 * @param params : String[]
	 * @return String : reflects the outcome of the method
	 */
	public static String chatMessage(String[] params) 
	{
		String sender = params[0];
		String text = params[1];
		System.out.println("in server manager chatMessage func" );
		if (!Server.userMap.containsKey(sender)) 
			return Message.chatMessageFailure(sender);
		else 
		{
			System.out.println("textfire! " + text + " to " + sender);
			Server.chatroom.textFire(sender, text);
			return Message.chatMessageSuccess(sender, text);
			
		}
			
	}
	
	
	
	public static String startDrag(String[] params)
	{
		String stackName = params[0];
		String userName = params[1];
		if (Server.stackUserMap.containsKey(userName)) 
			return Message.startDragFailure();
		else
		{
			Server.stackUserMap.put(stackName, userName);
			broadcast(Message.startDrag(stackName, userName));
			return Message.startDragSuccessful();
		}
	}
	
	
	public static void broadcast(String msg)
	{
		for (PrintWriter pw : Server.clientWriters.values()) {
			pw.println(msg);
			
			//Server.clientWriters.get(user.getUserName()).println(msg));
		}
	}
	
}
